{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 上周学习内容\n",
    "* A.列表入门\n",
    "* B.列表学习与运用\n",
    "* C.用户输入与格式化打印\n",
    "\n",
    "## 本周学习内容\n",
    "* A.列表的基本方法\n",
    "* B.列表的高级方法_copy()\n",
    "* C.列表的高级方法_枚举"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_id = [10001,10002,10003,10004,10008,10001,10009,10010,10001]\n",
    "# 思考，如何取出所有位置10001的index？\n",
    "names = [\"Mike\", \"Mary\", \"Jan\", \"Jack\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 列表的基本方法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 增加的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Mike', 'Mary', 'Jan', 'Jack', 1]\n",
      "insert之后的names： ['Mike', 'yougege', 'Mary', 'Jan', 'Jack', 1]\n"
     ]
    }
   ],
   "source": [
    "# 1.增加 append()  增加在list的末尾\n",
    "names.append(1)\n",
    "print(names)\n",
    "# 2.插入 insert() 可以指定位置增加元素\n",
    "names.insert(1,'yougege')\n",
    "print(\"insert之后的names：\",names)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 删除方法 remove  和 pop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remove删除的结果： ['Mike', 'yougege', 'Mary', 'Jan', 'Jack', 1]\n",
      "pop()方法后的names: ['Mike', 'yougege', 'Mary', 'Jan', 'Jack']\n"
     ]
    }
   ],
   "source": [
    "# 2. 删除 remove  和 pop\n",
    "# 优势：如果删除list中多个元素时，remove比较合适\n",
    "\"\"\"\n",
    "Remove first occurrence of value.\n",
    "Raises ValueError if the value is not present.\n",
    "\"\"\"\n",
    "# 2.1 remove()一般情况下 使用remove先用in做判断\n",
    "if 10001 in names:\n",
    "    names.remove(10001)\n",
    "print(\"remove删除的结果：\",names)\n",
    "\n",
    "\n",
    "# 2.2 pop() 删除位置为1的这个元素\n",
    "names.pop(-1)\n",
    "print(\"pop()方法后的names:\",names)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### index()索引 查看该元素如10001 在stu_id中第一个出现的位置索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "stu_id = [10001,10002,10003,10004,10008,10001,10009,10010,10001]\n",
    "print(stu_id.index(10001))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### clear()清除所有数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "清除names列表后的值： []\n"
     ]
    }
   ],
   "source": [
    "names.clear()\n",
    "print(\"清除names列表后的值：\",names)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### count()查看列表元素出现的个数，（简单应用）统计分数出现的次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "count方法的结果: 3\n",
      "[10001, 10002, 10003, 10004, 10008, 10001, 10009, 10010, 10001]\n"
     ]
    }
   ],
   "source": [
    "print(\"count方法的结果:\",stu_id.count(10001))\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### extend() 是给指定的list进行扩充"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10001, 10002, 10003, 10004, 10008, 10001, 10009, 10010, 10001, 10001, 10008, 10001, 10009, 10010, 10001]\n"
     ]
    }
   ],
   "source": [
    "names.extend(stu_id)\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 列表的其他方法(Sequence Types 的一些方法)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### len()查看list长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "names的长度/names的列表元素个数： 15\n"
     ]
    }
   ],
   "source": [
    "print(\"names的长度/names的列表元素个数：\",len(names))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### in 查看元素是否存在list当中,结果为布尔值，False/True\n",
    "* 一般在操作list的修改和删除时会先判断"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "print(\"zhichao\" in names)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 列表的高级方法_copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu_id: [10001, 10002, 10003, 10004, 10008, 10001, 10009, 10010, 10001] \n",
      " stu_id_matedata: [10001, 10002, 10003, 10004, 10008, 10001, 10009, 10010, 10001]\n"
     ]
    }
   ],
   "source": [
    "stu_id = [10001,10002,10003,10004,10008,10001,10009,10010,10001]\n",
    "\n",
    "# copy()很重要\n",
    "# 思考，赋值是真正的复制么？ 赋值(=) <=> copy? 不等价\n",
    "stu_id_matedata = stu_id\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 实验 1 赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu_id: [10001, 10003, 10004, 10008, 10001, 10009, 10010, 10001] \n",
      " stu_id_matedata: [10001, 10003, 10004, 10008, 10001, 10009, 10010, 10001]\n"
     ]
    }
   ],
   "source": [
    "stu_id.pop(1)\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 实验2 copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu_id: [10001, 10004, 10008, 10001, 10009, 10010, 10001] \n",
      " stu_id_matedata: [10001, 10003, 10004, 10008, 10001, 10009, 10010, 10001]\n"
     ]
    }
   ],
   "source": [
    "stu_id_matedata = stu_id.copy()\n",
    "stu_id.pop(1)\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 实验3 切片实现复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu_id: [10001, 10008, 10001, 10009, 10010, 10001] \n",
      " stu_id_matedata: [10001, 10004, 10008, 10001, 10009, 10010, 10001]\n"
     ]
    }
   ],
   "source": [
    "stu_id_matedata = stu_id[:]\n",
    "stu_id.pop(1)\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 列表的高级方法_枚举"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_id = [10001,10002,10003,10004,10008,10001,10009,10010,10001]\n",
    "# 思考，如何取出所有位置10001的index？\n",
    "for i in stu_id:\n",
    "    # 循环遍历stu_id的所有内容\n",
    "    if i == 10001:\n",
    "        # 判断如果元素为10001的时候,怎么取出索引值呢？\n",
    "        print()\n",
    "## 一般方法?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 10001\n",
      "5 10001\n",
      "8 10001\n",
      "我们想要删除pop()的值： [0, 5, 8]\n"
     ]
    }
   ],
   "source": [
    "# 枚举:因为list其实不仅有values值，还有index索引，但for循环主要循环遍历其值，不遍历索引\n",
    "# 因此，有了枚举的方法，同时遍历两者\n",
    "# enumerate()\n",
    "pop_values_list = []  # append方法经常用于新建列表\n",
    "for index,item in enumerate(stu_id):\n",
    "    # print(index,item)\n",
    "    if item == 10001:\n",
    "        print(index,item)\n",
    "        pop_values_list.append(index)\n",
    "print(\"我们想要删除pop()的值：\",pop_values_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 列表方法小结\n",
    "\n",
    "> 1.list.append\t在列表末尾添加新的对象  \n",
    "> 2.list.count\t统计某个元素在列表中出现的次数  \n",
    "> 3.list.extend\t在列表末尾一次性追加另一个序列中的多个值（用新列表扩展原来的列表）  \n",
    "> 4.list.index\t从列表中找出某个值第一个匹配项的索引位置  \n",
    "> 5.list.insert\t将对象插入列表  \n",
    "> 6.list.pop\t   移除列表中的一个元素（默认最后一个元素），并且返回该元素的值   \n",
    "> 7.list.remove\t移除列表中某个值的第一个匹配项   \n",
    "> 8.list.clear()\t清空列表  \n",
    "> 9.list.copy()\t复制列表   \n",
    "> 10.list.reverse()反向列表中元素  \n",
    "> 11.list.sort\t   对原列表进行排序 "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
